package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> findPage(Integer pageNum, Integer pageSize, String name) {
        //1.构建分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //2.构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name), Dish::getName, name);
        //3.执行查询
        page = dishMapper.selectPage(page, qw);

        //4.遍历菜品
        for (Dish dish : page.getRecords()) {
            //查询分类并封装
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
            //查询口味并封装
            LambdaQueryWrapper<DishFlavor> dishFlavorQw = new LambdaQueryWrapper<>();
            dishFlavorQw.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorQw);
            dish.setFlavors(dishFlavorList);
        }
        //5.返回结果
        return page;
    }


    @Override
    public void save(Dish dish) {
        //1.保存菜品基本信息
        dishMapper.insert(dish);
        //遍历口味列表
        for (DishFlavor flavor : dish.getFlavors()) {
            //设置菜品id
            flavor.setDishId(dish.getId());
            //保存口味到数据库
            dishFlavorMapper.insert(flavor);
        }
    }

    @Override
    public Dish findById(Long id) {
        //根据菜品id查询菜品信息
        Dish dish = dishMapper.selectById(id);
        //再根据菜品id查询口味列表
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
        //将口味设置到菜品信息
        dish.setFlavors(dishFlavors);
        //返回菜品信息
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //直接更新菜品信息
        dishMapper.updateById(dish);
        //根据菜品id删除就口味列表
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        //遍历新口味列表
        for (DishFlavor flavor : dish.getFlavors()) {
            //设置菜品id
            flavor.setDishId(dish.getId());
            //保存数据库
            dishFlavorMapper.insert(flavor);
        }
    }

    @Override
    public void deleteById(List<Long> ids) {
        //判断售卖状态
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        qw.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("要删除的菜品售卖状态应该为停售状态");
        }
        //删除菜品
        dishMapper.deleteBatchIds(ids);
        //删口味
        LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
        qw2.in(DishFlavor::getId, ids);
        dishFlavorMapper.delete(qw2);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        //修改实体
        Dish dish = new Dish();
        dish.setStatus(status);
        //条件修改
        dishMapper.update(dish, qw);
    }

    @Override
    public List<Dish> fingdishByName(String name) {
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.likeLeft(Dish::getName,name);
        List<Dish> dishes = dishMapper.selectList(lqw);
        return dishes;
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //1.组装查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, 1);
        List<Dish> dishes = dishMapper.selectList(wrapper);

        for (Dish dish : dishes) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavors);
        }

        return dishes;
    }

    @Override
    public List<Dish> findSteaml(Long id) {

        return null;
    }
}
